package com.huayue.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.huayue.entity.Menu;
import com.huayue.entity.Role;
import com.huayue.util.PageHelper;
import com.mysql.DBHelper;

/**
 * 菜单
 */
@Component
public class MenuDao implements BaseDao<Menu> {
    @Override
    public int insert(Menu entity) {
        return DBHelper.executeUpdate("insert into menu (parentid, name, url, icon) values (?, ?, ?, ?);",
            entity.getParentid(), entity.getName(), entity.getUrl(), entity.getIcon());
    }

    @Override
    public int update(Menu entity) {
        return DBHelper.executeUpdate("update menu set parentid=?,name=?,url=?,icon=? where id=?", entity.getParentid(),
            entity.getName(), entity.getUrl(), entity.getIcon(), entity.getId());
    }

    @Override
    public int delete(Menu entity) {
        return DBHelper.executeUpdate("update menu set is_deleted=1 where id=?", entity.getId());
    }

    @Override
    public List<Map<String, Object>> findAll(Object... obj) {
        return DBHelper.executeQuery("select * from menu where is_deleted=0");
    }

    public List<Map<String, Object>> findAllTOTree() {
        String sql = "select m.id,m.name label,m.parentid from menu m where is_deleted=0";
        return DBHelper.executeQuery(sql);
    }

    @Override
    public List<Map<String, Object>> findPage(PageHelper page, Object... obj) {
        String sql =
            "select a.name parentname,b.* from menu a right JOIN menu b on b.parentid=a.id where 1=1 and b.is_deleted=0 ";
        if (obj[0] != null & "0".equals(obj[0].toString())) {
            sql += " and b.parentid=0";
        } else if (obj[0] != null & "1".equals(obj[0].toString())) {
            sql += " and b.parentid!=0";
        }
        return DBHelper.executeQuery(sql + " limit ?,? ", page.getBegin(), page.getPageSize());
    }

    @Override
    public Map<String, Object> findOne(Menu entity) {
        return DBHelper.executeQuery("select * from menu where 1=1 and is_deleted=0 and id=? ", entity.getId()).get(0);
    }

    @Override
    public int count(Object... obj) {
        String sql =
            "select count(*) counts from menu a right JOIN menu b on b.parentid=a.id where 1=1 and b.is_deleted=0 ";
        if (obj[0] != null & "0".equals(obj[0].toString())) {
            sql += " and b.parentid=0";
        } else if (obj[0] != null & "1".equals(obj[0].toString())) {
            sql += " and b.parentid!=0";
        }
        return Integer.valueOf(DBHelper.executeQuery(sql).get(0).get("counts").toString());
    }

    public List<Map<String, Object>> findAllTopMenu() {
        return DBHelper.executeQuery("select * from menu where is_deleted=0 and parentid=0");
    }

    public List<Map<String, Object>> findAllByRoleId(Role role) {
        String sql = "select me.id,me.parentid,me.name,me.url,me.icon from menu me"
            + " inner join authority au on me.id=au.menu_id inner join role ro on au.role_id=ro.id "
            + "where ro.id=? and au.is_deleted=0 and me.is_deleted=0";
        return DBHelper.executeQuery(sql, role.getId());
    }

    public List<Map<String, Object>> findAllByRoleIdNoTree(Role role) {
        String sql = "select me.id,me.parentid,me.name,me.url,me.icon from menu me"
            + " inner join authority au on me.id=au.menu_id inner join role ro on au.role_id=ro.id "
            + "where ro.id=? and au.is_deleted=0 and me.is_deleted=0 and me.parentid>0 or me.id=1";
        return DBHelper.executeQuery(sql, role.getId());
    }
}
